package cn.gary.oaserver.service.impl;

import cn.gary.oaserver.config.utils.ConstansOA;
import cn.gary.oaserver.config.utils.MenuStreamUtil;
import cn.gary.oaserver.config.utils.RedisUtil;
import cn.gary.oaserver.entity.TAdmin;
import cn.gary.oaserver.entity.TMenu;
import cn.gary.oaserver.entity.TMenuRole;
import cn.gary.oaserver.mapper.TMenuMapper;
import cn.gary.oaserver.service.ITMenuRoleService;
import cn.gary.oaserver.service.ITMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gary
 * @since 2024-03-15
 */
@Service
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu> implements ITMenuService {

    @Autowired
    MenuStreamUtil menuStreamUtil;

    @Resource
    RedisTemplate<String,Object> redisTemplate;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ITMenuRoleService menuRoleService;

    Lock rtl=new ReentrantLock (  );

    // 通过登录用户ID查询菜单列表
    @Override
    public List<TMenu> getMenusByAdminId() {
        TAdmin admin = (TAdmin) (SecurityContextHolder.getContext ( ).getAuthentication ( ).getPrincipal ( ));
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        // 2、从redis中获取菜单数据
        List<TMenu> menus = (List<TMenu>)valueOperations.get("menu_" + admin.getId ());
        // 3、判断当前获取到的菜单列表数据是否为空，如果为空就去数据库中获取
        if (CollectionUtils.isEmpty(menus)) {
            menus = baseMapper.getMenusByAdminId(admin.getId ());
            // 3.1 将从数据库中获取到的菜单列表数据保存到redis中
            valueOperations.set("menu_" + admin.getId (), menus);
        }
        return menus;
    }

    @Override
    public List<TMenu> getMenusByRoleId(Integer rid) {
        return baseMapper.getMenusByRoleId ( rid );
    }

    @Override
    public List<TMenu> getMenus() {
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue ( );
        List<TMenu> menuList = (List<TMenu>) opsForValue.get ( ConstansOA.REDIS_MENU );
        if (CollectionUtils.isEmpty ( menuList )){
            menuList= menuStreamUtil.getAllMenus ( );
            opsForValue.set ( ConstansOA.REDIS_MENU,menuList );
        }
        return menuList;
    }

    @Override
    public boolean delMenusById(Integer id) {
        rtl.lock ();
        Integer[] ids = baseMapper.getMenusById ( id );
        boolean flag=false;
        for (Integer delId : ids) {
            System.out.println ("删除的id:"+delId );
            QueryWrapper<TMenuRole> qw=new QueryWrapper<> (  );
            qw.in("mid",delId);
            if (menuRoleService.remove ( qw )) {
                if (removeById ( delId )){
                    System.out.println ("删除" );
                    flag=true;
                }
            }
        }
        if (flag){
            redisUtil.clearMenuListFromRedis ();
        }
        rtl.unlock ();
        return flag;
    }

    @Override
    public boolean saveOrUpdateMenu(TMenu menu) {
        if (this.saveOrUpdate ( menu )){
            redisUtil.clearMenuListFromRedis ();
            return true;
        }
        return false;
    }

    @Override
    public List<TMenu> getMenusWithRole() {
        return baseMapper.getMenusWithRole ();
    }



}
